package com.hyt.it.ogt.kq.service.bm.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.utils.KqStringUtils;
import com.hyt.it.ogt.kq.service.bm.mapper.InvoiceResultMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.Invoice;
import com.hyt.it.ogt.kq.service.bm.model.entity.InvoiceAddr;
import com.hyt.it.ogt.kq.service.bm.model.entity.InvoiceItem;
import com.hyt.it.ogt.kq.service.bm.model.entity.InvoiceResult;
import com.hyt.it.ogt.kq.service.bm.model.vo.InvoiceResultVo;
import com.hyt.it.ogt.kq.service.bm.model.vo.InvoiceSettingVo;
import com.hyt.it.ogt.kq.service.bm.model.vo.InvoiceVo;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceAddrService;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceItemService;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceResultService;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceService;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceSettingService;
import com.hyt.loginfo.util.UserIdToNameUtil;

/**
 * <p>
 * 发票回调信息 服务实现类
 * </p>
 *
 * @author liteng
 * @since 2020-09-01
 */
@Service
public class InvoiceResultServiceImpl extends BaseServiceImpl<InvoiceResultMapper, InvoiceResult> implements IInvoiceResultService {

    @Autowired
    IInvoiceService iInvoiceService ;

    @Autowired
    protected UserIdToNameUtil<InvoiceVo> userIdToNameUtil;

    @Autowired
    IInvoiceAddrService iInvoiceAddrService ;
    @Autowired
    IInvoiceItemService iInvoiceItemService;
    @Autowired
    IInvoiceSettingService iInvoiceSettingService;

    @Override
    public InvoiceResultVo queryInvoiceResult(String invoiceId){
        Invoice invoice = iInvoiceService.getById(invoiceId);
        InvoiceResultVo vo = new InvoiceResultVo();
        BeanUtils.copyProperties(invoice,vo);
        QueryWrapper<InvoiceResult> wrapper = new QueryWrapper<InvoiceResult>();
        wrapper.eq("bm_invoice_id",invoiceId);
        InvoiceResult result = this.getOne(wrapper);
        vo.setPdfUrl(result.getPdfUrl());
        vo.setInnerRsCode(result.getInnerRsCode());
        vo.setRsMsg(result.getRsMsg());
        QueryWrapper<InvoiceAddr> addrWrapper = new QueryWrapper<InvoiceAddr>();
        addrWrapper.eq("bm_invoice_id",invoiceId);
        InvoiceAddr addr = iInvoiceAddrService.getOne(addrWrapper);
        BeanUtils.copyProperties(addr,vo);
        QueryWrapper<InvoiceItem> itemWrapper = new QueryWrapper<InvoiceItem>();
        itemWrapper.eq("bm_invoice_id",invoiceId);
        InvoiceItem item = iInvoiceItemService.getOne(itemWrapper);
        vo.setXmmc(item.getXmmc());

        InvoiceSettingVo invoiceSettingVo = iInvoiceSettingService.queryInvoice(invoice.getOfficeId(), "1");
        if (null != invoiceSettingVo) {
            vo.setTelphone(invoiceSettingVo.getPhone());
            if (KqStringUtils.isEmpty(vo.getAddrDetail())) {
                vo.setAddrDetail(invoiceSettingVo.getAddrDetail());
                vo.setSettingSate(invoiceSettingVo.getAddrDetail());
            }
        }
        return vo;
    }
}
